package com.yongbao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yongbao.common.PageInfo;
import com.yongbao.common.utils.AssertUtil;
import com.yongbao.model.dtomapper.DishOrderBookDtoMapper;
import com.yongbao.model.domain.DishOrderBookDO;
import com.yongbao.mapper.DishOrderBookMapper;
import com.yongbao.model.request.DishOrderBookRequest;
import com.yongbao.model.util.DishOrderBookConvert;
import com.yongbao.service.DishOrderBookService;
import com.yongbao.model.vo.DishOrderBookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DishOrderBookServiceImpl extends ServiceImpl<DishOrderBookMapper, DishOrderBookDO> implements DishOrderBookService {
    @Autowired
    private DishOrderBookMapper dishOrderBookMapper;

    @Override
    public DishOrderBookVO getByKey(long id) {
        DishOrderBookVO dishOrderBookVO = DishOrderBookDtoMapper.INSTANCE.do2VO(getById(id));
        return dishOrderBookVO;
    }

    @Override
    public PageInfo<DishOrderBookVO> pageQuery(DishOrderBookRequest request) {
        long count = dishOrderBookMapper.pageCount(request);
        List<DishOrderBookDO> list = dishOrderBookMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DishOrderBookVO> dishOrderBookVOList = DishOrderBookConvert.buildDishOrderBookVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), dishOrderBookVOList, count);
    }

    @Override
    public List<DishOrderBookVO> listQuery(DishOrderBookRequest request) {
        List<DishOrderBookDO> list = dishOrderBookMapper.selectDOList(DishOrderBookDtoMapper.INSTANCE.request2DO(request));
        List<DishOrderBookVO> dishOrderBookVOS = DishOrderBookConvert.buildDishOrderBookVoList(list);
        return dishOrderBookVOS;
    }

    @Override
    public Boolean add(DishOrderBookRequest request) {
        save(DishOrderBookDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(DishOrderBookRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(DishOrderBookDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean delByIds(String ids) {
        dishOrderBookMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
